<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <link rel="stylesheet" href="/public/asset/bootstrap.min.css">
  <link rel="stylesheet" href="/public/asset/bootstrap-icons-141.css">
  <link rel="stylesheet" href="/public/asset/style.css">
  
  <script src="/public/asset/jquery.min.js"></script>
  <script src="/public/asset/dayjs.min.js"></script>
  <script src="/public/asset/bootstrap.bundle.min.js"></script>
  <script src="/public/asset/clipboard.min.js"></script>
  <script src="/public/asset/util.js"></script>
  
  <title>Publish - iPelago</title>
</head>
<body>
  <div id="root" class="container" style="max-width: 775px; min-width: 400px;"></div>

<script>

const Loading = CreateLoading();
const Alerts = CreateAlerts();

const TitleArea = {
  view: () => m('div').addClass('d-flex justify-content-between align-items-center my-5').append([
    m('div').attr({id:'title'}).addClass('display-6').text('Publish'),
    m('a').attr({href:'/public/home.html',title:'home'}).addClass('btn btn-outline-dark').append(
      m('i').addClass('bi bi-house-door')
    ),
  ]),
};

const CopyBtn = cc('button', 'copy');
const DownloadBtn = cc('a');
const ButtonsArea = cc('div', null, [
  m(CopyBtn).text('Copy').addClass('btn btn-outline-primary').attr({type:'button'}),
  m(DownloadBtn).text('Download').addClass('btn btn-outline-primary ms-2')
    .attr({type:'button',download:'newsletter.json',href:'/public/newsletter.json'}),
  m('a').text('Edit').addClass('btn btn-outline-secondary ms-2')
    .attr({href:'/public/my-messages.html'}),
]);

const Newsletter = cc('textarea');

$('#root').append([
  m(TitleArea),
  m(Loading),
  m(Alerts).addClass('my-3'),
  m(ButtonsArea).hide(),
  m(Newsletter).addClass('mt-3 mb-5 form-control').hide(),
]);

init();

function init() {
  ajax({method:'GET',url:'/public/newsletter.json',alerts:Alerts,responseType:'text'},
      (newsletter) => {
        $(ButtonsArea.id).show();
        $(Newsletter.id).show().val(newsletter)
          .css('height', $(Newsletter.id).prop('scrollHeight'))
          .prop({disabled:true});
      }, null, () => {
        $(Loading.id).hide();
      });
}

const clipboard = new ClipboardJS(CopyBtn.id, {
  text: () => { return $(Newsletter.id).val(); }
});
clipboard.on('success', () => {
  $(CopyBtn.id).text('copied').removeClass('btn-outline-primary').addClass('btn-outline-secondary');
});
clipboard.on('error', e => {
  console.error('Action:', e.action);
  console.error('Trigger:', e.trigger);
  Alerts.insert('danger', '复制失败，详细信息见控制台');
});

</script>
</body>
</html>